<?php


namespace app\common\business;

use app\common\model\mysql\Category as CategoryModel;
use think\Exception;

class Category
{
    public $model = null;
    public function __construct()
    {
        $this->model = new CategoryModel();
    }

    public function add($data){
        $data['status'] = config("status.mysql.table_normal");
        $name = $data['name'];
        //根据$name查询数据库存在给出提示
        $res = $this->model->where('name', '=', $name)->find();
        if(!empty($res)){
            throw new Exception("类名已存在");
        }
        try {
            $this->model->save($data);
        }catch(Exception $e){
            //记录日志
            throw new Exception("服务内部异常");
        }
        return $this->model->id;
    }

    public function getNormalCategorys(){
        $field = "id, name, pid";
        $categorys = $this->model->getNormalCategorys($field);
        if(!$categorys){
            return $categorys;
        }
        $categorys = $categorys->toArray();
        return $categorys;
    }

    public function getNormalAllCategorys(){
        $field = "id as category_id, name, pid";
        $categorys = $this->model->getNormalCategorys($field);
        if(!$categorys){
            return $categorys;
        }
        $categorys = $categorys->toArray();
        return $categorys;
    }

    public function getLists($data, $num){
        $list = $this->model->getLists($data, $num);
        if(!$list){
            return [];
        }
        $result = $list->toArray();
        $result['render'] = $list->render();
        //1.获取列表pid 2.in mysql 求count
        $pids = array_column($result['data'], "id");
        if($pids){
            $idCountResult = $this->model->getChildCountInPids(['pid' => $pids]);
            $idCountResult = $idCountResult->toArray();
            $idCounts = [];
            foreach($idCountResult as $countResult){
                $idCounts[$countResult['pid']] = $countResult['count'];
            }
        }
        if($result['data']){
            foreach($result['data'] as $k => $v){
                $result['data'][$k]['childCount'] = $idCounts[$v['id']] ?? 0;
            }
        }
        return $result;
    }

    public function listorder($id, $listorder){
        $res = $this->model->getById($id);
        if(!$res){
            throw new Exception("不存在该条记录");
        }
        $data = [
            "listorder" => $listorder,
        ];
        try {
            $res = $this->model->updateById($id, $data);
        } catch(Exception $e){
            //记录日志
            return false;
        }
        return $res;
    }
    public function status($id, $status){
        $res = $this->model->getById($id);
        if(!$res){
            throw new Exception("不存在该条记录");
        }
        if($res['status'] == $status){
            throw new Exception("状态修改前和修改后一样没有任何意义哦");
        }
        $data = [
            "status" => intval($status),
        ];
        try{
            $res = $this->model->updateById($id, $data);
        } catch(Exception $e){
            //记录日志
            return false;
        }
        return $res;
    }

    /**
     * 获取一级分类的内容
     * @param int $pid
     * @param string $field
     * @return array
     */
    public function getNormalByPid($pid = 0, $field = "id,name,pid"){
        try{
            $res = $this->model->getNromalByPid($pid, $field);
        }catch(Exception $e){
            // 记录日志
            return [];
        }
        $res = $res->toArray();
        return $res;
    }
}
